<template>
	<view>
		<!-- 搜索 -->
		<!-- 		<view class="uni-search-box">
			<uni-search-bar ref="searchBar" v-model="searchValue" :placeholder="inputPlaceholder" radius="100" disabled
				cancelButton="none" />
			<view class="cover-search-bar" @click="searchClick"></view>
		</view> -->

		<!-- <view class="header">我的购物车</view> -->

		<!-- 通知栏 -->
		<view class="notice-bar">
			<uni-notice-bar :speed="50" show-icon scrollable show-close :text="noticeText" v-show="noticeIsShow" />
		</view>

		<!-- 轮播图 -->
		<view class="banner-box">
			<unicloud-db ref="bannerdb" v-slot:default="{data, loading, error, options}" collection="opendb-banner"
				field="_id,bannerfile,open_url,title">
				<swiper class="swiper-box" circular indicator-dots="true" autoplay="true" :interval="3000"
					duration="2000">
					<swiper-item v-for="(item,index) in data">
						<image class="banner-image" :src="item.bannerfile.url" mode="aspectFill"
							@click="clickBannerItem(item)" />
					</swiper-item>
				</swiper>
			</unicloud-db>
		</view>

		<!-- 首页分类区 -->
		<view class="primary-classification-box">
			<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" collection="opendb-mall-categories">
				<uni-row>
					<uni-col v-if="index<4" width="100%" v-for="(item, index) in data" :span="6">
						<view class="primary-classification-area" @click="clickPriClas(item,index)">
							<image class="primary-classification-image" :src="item.icon.url"></image>
							<text class=" primary-classification-text">{{item.name}}</text>
						</view>
					</uni-col>
				</uni-row>
				<view style="display: block;height: 10px;width: 100%;"></view>
				<uni-row width="100%">
					<uni-col v-if="index>=4" v-for="(item, index) in data" :span="6">
						<view class="primary-classification-area" @click="clickPriClas(item,index)">
							<image class="primary-classification-image" :src="item.icon.url"></image>
							<text class="primary-classification-text">{{item.name}}</text>
						</view>
					</uni-col>
				</uni-row>
			</unicloud-db>
		</view>

		<!-- 商品列表 -->
		<view class="goods-box">
			<scroll-view scroll-y style="height: 100%;">
				<view v-for="(item,index) in goodsList">
					<uni-row class="row">
						<uni-col :span="8">
							<view @click="clickGoods(item._id)">
								<image class="goods-img" :src="item.goods_banner_imgs[0].url"></image>
							</view>
						</uni-col>
						<uni-col :span="16">
							<view class="goods-desc-box" @click="clickGoods(item._id)">
								<p>
									{{item.name}}
								</p>
								<text>
									{{item.goods_desc}}
								</text>
							</view>
							<view class="price-box">
								<text class="price-font">
									<text class="yuan-font">回收价格￥</text>
									<text style=" font-size: 40rpx;">
										{{convert(item.goods_price).integer}}
									</text>
									.
									<text style="font-size: 32rpx;">
										{{convert(item.goods_price).point}}
									</text>
								</text>
							</view>
							<!-- 							<uni-icons type="cart" size="30" class="addCart" @click="toSale(item._id)"
								color="#FF5000"></uni-icons> -->
							<img src="/static/sale.png" class="addCart" @click="toSale(item._id)" alt="出售">
						</uni-col>
					</uni-row>
					<view style="height: 10px;"></view>
				</view>
				<view style="height: 30px;"></view>
			</scroll-view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				lastDate: null,
				noticeIsShow: true,
				noticeText: '',
				goodsList: [],
				inputPlaceholder: '搜索',
				searchValue: ''
			}
		},
		mounted() {
			//页面加载完成调用,通过引用调用组件的方法
			this.getNotice()
			this.getGoods(this.lastDate)
		},
		// 下拉刷新
		onPullDownRefresh() {
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		//触底加载数据
		onReachBottom() {
			if (this.lastDate != null) {
				this.getGoods(this.lastDate)
			}
		},
		methods: {
			//点击搜索框
			searchClick(e) {
				uni.hideKeyboard();
				uni.navigateTo({
					url: '/pages/list/search/search',
					animationType: 'fade-in'
				});
			},
			/* 点击分类的处理 */
			clickPriClas(item, index) {
				uni.reLaunch({
					url: '/pages/grid/grid?cate=' + item.name + '&index=' + index
				})
			},
			/**
			 * 点击banner的处理
			 */
			clickBannerItem(item) {
				uni.showModal({
					content: '',
					showCancel: false
				});
			},
			// 获取通知栏数据
			async getNotice() {
				const db = uniCloud.database();
				//只获取最新的一条数据，数据库有多条数据也只要展示一条
				let res = await db.collection('notice-bar').where({
					area: 'home'
				}).limit(1).get()
				this.noticeIsShow = res.result.data[0].is_show
				this.noticeText = res.result.data[0].text
			},
			getLastDate(lastDate) {
				this.lastDate = parseInt(lastDate)
			},
			convert(money) {
				return {
					integer: Math.floor(money / 100),
					point: money % 100
				}
			},
			async clickGoods(id) {
				let res = await uniCloud.importObject('goods').getById(id)
				uni.navigateTo({
					url: '/pages/details/details?item=' + encodeURIComponent(JSON.stringify(res[0]))
				})
			},
			async getGoods(lastDate) {
				let res = await uniCloud.importObject('goods').getAllGoods(this.lastDate)
				if (res.length == 0) {
					uni.showToast({
						title: '没有了哦~',
						icon: "loading",
						duration: 800
					})
					return;
				}
				this.lastDate = res[res.length - 1].auto_createdate

				this.goodsList = [...this.goodsList, ...res]
			},
			//预约回收
			async toSale(id) {
				let res = await uniCloud.importObject('goods').getById(id)
				uni.navigateTo({
					url: '/pages/order/order?goods=' + encodeURIComponent(JSON.stringify(res[0]))
				})
			},
		},

	}
</script>

<style>
	/* 通知栏 */
	.notice-bar {
		top: 0;
		width: 750rpx;
		position: fixed;
		z-index: 3;
	}

	/* 轮播图 */
	.banner-image {
		width: 710rpx;
		height: 400rpx;
		border-radius: 5px;
	}

	.swiper-box {
		height: 400rpx;
	}

	.banner-box {
		background-color: #ffffff;
		width: 710rpx;
		margin: 0 auto 0rpx;
		border-radius: 18rpx;
	}

	/* 一级分类 */
	.primary-classification-box {
		background-color: #ffffff;
		width: 710rpx;
		margin: 10px auto 0rpx;
		border-radius: 18rpx;
		font-size: 20rpx;
	}

	.primary-classification-image {
		height: 45rpx;
		width: 45rpx;
	}

	.primary-classification-text {
		display: block;
	}

	.primary-classification-area {
		text-align: center;
	}

	/* 商品区 */
	.goods-box {
		width: 710rpx;
		margin: 10px auto 0rpx;
	}

	.goods-desc-box {
		height: 70%;
		width: 85%;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.goods-desc-box text {
		font-size: 12px;
		color: #a2a2a2;
	}

	.goods-img {
		height: 200rpx;
		width: 200rpx;
		border-radius: 8px
	}

	.row {
		height: 200rpx;
		border-radius: 18rpx;
		background-color: #ffffff;
	}

	.yuan-font {
		color: #FF5000;
		font-size: 32rpx;
	}

	.price-box {
		height: 30%;
		width: 85%;
		word-wrap: break-word;
		position: absolute;
		bottom: 0rpx;
	}

	.x1 {
		font-size: 32rpx;
		margin: 10rpx;
		width: 50rpx;
		height: 50rpx;
		border: #8a8a8a 1px solid;
		border-radius: 18rpx;
		display: inline-block;
		line-height: 50rpx;
		text-align: center;
	}

	.addCart {
		height: 60rpx;
		width: 60rpx;
		position: absolute;
		bottom: 10rpx;
		right: 10rpx;
	}

	/* 搜索 */
	.uni-search-box {
		background-color: #FFFFFF;
		position: sticky;
		height: 50px;
		top: 0;
		left: 0;
		/* #ifndef APP-PLUS */
		z-index: 9;
		/* #endif */
		/* #ifdef MP-WEIXIN */
		width: 580rpx;
		/* #endif */
	}

	.cover-search-bar {
		height: 50px;
		position: relative;
		top: -50px;
		margin-bottom: -50px;
		/* #ifndef APP-NVUE */
		z-index: 999;
		/* #endif */
	}
</style>